﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SimpleWebApp.Demo02
{
    public sealed class StringContentMiddlewareV2
    {
        private readonly RequestDelegate _next;
        private readonly string _contents;
        private readonly bool _forewardToNext;

        // 第一个参数是注入的
        // 后面两个参数是通过 UseMiddleware 方法传递的
        public StringContentMiddlewareV2(RequestDelegate next, string contents, bool forewardToNext = true)
        {
            _next = next;
            _contents = contents;
            _forewardToNext = forewardToNext;
        }

        // 这个是根据 Middleware 约定命名的方法
        public async Task Invoke(HttpContext context)
        {
            await context.Response.WriteAsync(_contents);
            if (_forewardToNext)
            {
                await _next(context);
            }
        }
    }
}
